﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using agent02.com.somee.g02airplane;

namespace agent02.Model
{
    public class AIRPORT
    {
        public Int32 ID { get; set; }
        public string Name { get; set; }
        public string Country { get; set; }
        public string Area { get; set; }
    }

    public class FLIGHT
    {
        public Int32 ID { get; set; }
        public string TenTuyen { get; set; }
        public bool LoaiChuyen { get; set; }
        public DateTime NgayKhoiHanh { get; set; }
        public DateTime NgayDen { get; set; }
        public DateTime NgayVe { get; set; }
        public Int32 ChoConTrong { get; set; }
        public string SanBayDi { get; set; }
        public string SanBayDen { get; set; }
    }

    public class AirplaneSearchModel
    {
        public string sanBayDi { get; set; }
        public string sanBayDen { get; set; }
        public SelectList sanBayDiList { get; set; }
        public SelectList sanBayDenList { get; set; }
        public AirplaneSearchModel()
        {
            WebService_AirPlane ws = new WebService_AirPlane();

            DataSet sanBayDiDS = ws.DanhSachSanBayDi();
            DataSet sanBayDenDS = ws.DanhSachSanBayDen();
            DataTable sanBayDiDT = sanBayDiDS.Tables[0];
            DataTable SanBayDenDT = sanBayDenDS.Tables[0];

            sanBayDiList = new SelectList(this.ConvertToAIRPORT(sanBayDiDT), "ID", "Name");
            sanBayDenList = new SelectList(this.ConvertToAIRPORT(SanBayDenDT), "ID", "Name");
        }

        public IEnumerable<AIRPORT> ConvertToAIRPORT(DataTable dt)
        {
            foreach (DataRow dr in dt.Rows)
            {
                yield return new AIRPORT
                {
                    ID = Convert.ToInt32(dr[0]),
                    Name = Convert.ToString(dr[1]),
                    Country = Convert.ToString(dr[2]),
                    Area = Convert.ToString(dr[3])
                };
            }
        }
    }

    public class AirplaneSearchResultModel
    {
        public IEnumerable<FLIGHT> ConvertToFLIGHT(DataTable dt)
        {
            foreach (DataRow dr in dt.Rows)
            {
                DateTime nkh = new DateTime();  // Ngay Khoi Hanh
                DateTime nd = new DateTime();   // Ngay Den
                DateTime nv = new DateTime();   // Ngay Ve
                if (dr[3] != DBNull.Value)
                {
                    nkh = Convert.ToDateTime(dr[3]);
                }
                if (dr[4] != DBNull.Value)
                {
                    nd = Convert.ToDateTime(dr[4]);
                }
                if (dr[5] != DBNull.Value)
                {
                    nv = Convert.ToDateTime(dr[5]);
                }
                yield return new FLIGHT
                {
                    ID = Convert.ToInt32(dr[0]),
                    TenTuyen = Convert.ToString(dr[1]),
                    LoaiChuyen = Convert.ToBoolean(dr[2]),
                    NgayKhoiHanh = nkh,
                    NgayDen = nd,
                    NgayVe = nv,
                    ChoConTrong = Convert.ToInt32(dr[6]),
                    SanBayDi = Convert.ToString(dr[7]),
                    SanBayDen = Convert.ToString(dr[8])
                };
            }
        }
    }
}